/**
 * add_province
 * 
 * 增加一个省
 * 
 */
function add_province() {
    var province = $('#provinces').val().trim()
    console.log(province)
    url = '/mgt/addProvince/province/' + province

    $.post(
        url, {},
        function(res) {
            if (res.Code == 0) {
                flush_province()
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}


/**
 * del_province
 * 
 * 删除一个省
 * 
 * 
 **/
function del_province(id) {
    url = '/mgt/delProvince/id/' + id
    data = {}
    $.post(
        url,
        data,
        function(res) {
            console.log(res)
            if (res.Code == 0) {
                console.log(res)
                window.location.href = "/"
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}


/**
 * edit_province
 * 修改省的名称
 * 
 * */
function edit_province(id, name) {

    layer.prompt({
        title: '请输入要修改省的名字',
        value: name
    }, function(value, index, elem) {
        layer.close(index);
        console.log(value)
        url = '/mgt/editProvince/id/' + id + '/province/' + value
        data = {}
        $.post(
            url,
            data,
            function(res) {
                console.log(res)
                if (res.Code == 0) {
                    console.log(res)
                    window.location.href = "/"
                } else {
                    layer.msg(res.Msg, {
                        icon: 5
                    });
                }
            }, 'JSON');
    });
}

/**
 * add_city
 * 
 * 增加一个城市
 * 
 */
function add_city(par_id) {
    layer.prompt({
        title: '请输入要添加的城市',
    }, function(value, index, elem) {
        layer.close(index);
        console.log(value)
        console.log(par_id)
        url = '/mgt/addCity/par_id/' + par_id + '/city/' + value
        data = {}
        $.post(
            url,
            data,
            function(res) {
                if (res.Code == 0) {
                    flush_city(par_id)
                } else {
                    layer.msg(res.Msg, {
                        icon: 5
                    });
                }
            }, 'JSON');
    });

}

/**
 * del_city
 * 
 * 删除一个城市
 * 
 */
function del_city(id, par_id) {
    url = '/mgt/delCity/id/' + id
    data = {}
    $.post(
        url,
        data,
        function(res) {
            console.log(res)
            if (res.Code == 0) {
                console.log(res)
                flush_city(par_id)
                $('#area').empty()
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * edit_city
 * 
 * 编辑一个城市
 * 
 */
function edit_city(id, name, par_id) {
    layer.prompt({
        title: '请输入要修改城市的名字',
        value: name
    }, function(value, index, elem) {
        layer.close(index);
        console.log(value)
        url = '/mgt/editCity/id/' + id + '/city/' + value
        data = {}
        $.post(
            url,
            data,
            function(res) {
                console.log(res)
                if (res.Code == 0) {
                    flush_city(par_id)
                } else {
                    layer.msg(res.Msg, {
                        icon: 5
                    });
                }
            }, 'JSON');
    });
}

/**
 * add_area
 * 
 * 添加一个市区
 * 
 */
function add_area(par_id) {
    layer.prompt({
        title: '请输入要添加的市区',
    }, function(value, index, elem) {
        layer.close(index);
        console.log(value)
        console.log(par_id)
        url = '/mgt/addArea/area/' + value + '/par_id/' + par_id
        data = {}
        $.post(
            url,
            data,
            function(res) {
                if (res.Code == 0) {
                    flush_area(par_id)
                } else {
                    layer.msg(res.Msg, {
                        icon: 5
                    });
                }
            }, 'JSON');
    });
}

/**
 * del_area
 * 
 * 删除一个市区
 * 
 */
function del_area(id, par_id) {
    url = '/mgt/delArea/id/' + id
    data = {}
    $.post(
        url,
        data,
        function(res) {
            console.log(res)
            if (res.Code == 0) {
                flush_area(par_id)
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * edit_area
 * 
 * 编辑一个市区
 * 
 */
function edit_area(id, name, par_id) {
    layer.prompt({
        title: '请输入要修改市区的名字',
        value: name
    }, function(value, index, elem) {
        layer.close(index);
        url = '/mgt/editArea/id/' + id + '/area/' + value
        data = {
            'id': id,
            'area': value
        }
        $.post(
            url,
            data,
            function(res) {
                console.log(res)
                if (res.Code == 0) {
                    flush_area(par_id)
                } else {
                    layer.msg(res.Msg, {
                        icon: 5
                    });
                }
            }, 'JSON');
    });
}


/**
 * flush_province
 * 刷新省级列表重新加载数据
 * 
 */
function flush_province() {
    $("#province").empty();
    url = '/mgt/getProvinces/'
    $.post(
        url, {},
        function(res) {
            if (res.Code == 0) {
                var tmp_province = ''
                res.Data.forEach(row => {
                    tmp_province = tmp_province + "<h4 onclick='showCity(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_city(" +
                        row[0] + ")>+</button><button type='button' onclick=del_province(" + row[0] +
                        ")>-</button><button type='button' onclick=edit_province(" + row[0] + ",'" + row[1] +
                        "')>E</button></span></h4>"
                });
                $("#province").html(tmp_province);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}


/**
 * flush_city
 * 刷新城市列表重新加载数据
 * 
 */
function flush_city(par_id) {
    $("#city").empty();
    url = '/mgt/getCity/id/' + par_id
    $.post(
        url, {},
        function(res) {
            if (res.Code == 0) {
                var tmp_province = ''
                res.Data.forEach(row => {
                    tmp_province = tmp_province + "<h4 onclick='showArea(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_area(" +
                        row[0] + ")>+</button><button type='button' onclick=del_city(" + row[0] + "," + row[2] +
                        ")>-</button><button type='button' onclick=edit_city(" + row[0] + ",'" + row[1] + "'," + row[2] +
                        ")>E</button></span></h4>"
                });
                $("#city").html(tmp_province);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * flush_area
 * 刷新市区列表重新加载数据
 * 
 */
function flush_area(par_id) {
    $("#area").empty();
    url = '/mgt/getArea/id/' + par_id
    $.post(
        url, {},
        function(res) {
            if (res.Code == 0) {
                var tmp_area = ''
                res.Data.forEach(row => {
                    tmp_area = tmp_area + "<h4>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=del_area(" +
                        row[0] + "," + row[2] + ")>-</button><button type='button' onclick=edit_area(" + row[0] +
                        ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#area").html(tmp_area);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}



/**
 * showCity
 * 显示城市列表 
 * 
 * @param mixed $id 要查询的城市的父级ID
 */
function showCity(id) {
    url = '/mgt/getCity/id/' + id
    data = {}
    $.post(
        url,
        data,
        function(res) {
            if (res.Code == 0) {
                $("#city").empty();
                var tmp_city = ''
                res.Data.forEach(row => {
                    tmp_city = tmp_city + "<h4 onclick='showArea(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_area(" +
                        row[0] + ")>+</button><button type='button' onclick=del_city(" + row[0] +
                        "," + row[2] + ")>-</button><button type='button' onclick=edit_city(" + row[0] +
                        ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#city").html(tmp_city);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * showArea
 * 显示市区列表 
 * 
 * @param mixed $id 要查询的市区的父级ID
 */
function showArea(id) {
    url = '/mgt/getArea/id/' + id
    data = {}
    $.post(
        url,
        data,
        function(res) {
            if (res.Code == 0) {
                $("#area").empty();
                var tmp_area = ''
                res.Data.forEach(row => {
                    tmp_area = tmp_area + "<h4>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=del_area(" +
                        row[0] + "," + row[2] + ")>-</button><button type='button' onclick=edit_area(" +
                        row[0] + ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#area").html(tmp_area);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}